<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/12/1
 * Time: 0:39
 */

namespace App\Plugin\HostServer\Front;

use App\Plugin\HostServer\Front\Mail\EmailNotify;
use App\Plugin\HostServer\Models\PnHostSvDoing;
use App\Plugin\HostServer\Models\PnHostSvDomain;
use App\Plugin\HostServer\Models\PnHostSvDomainOrder;
use App\Plugin\HostServer\Models\PnHostSvOrder;
use App\Plugin\HostServer\Services\NotifyService;
use App\Services\DateServices;

class CollectController extends  BaseController
{
    public function index(){
        //找出今天没有通知的
        $res=$this->getOrder(100);
        try {
            if(empty($res))
            {
                echo '没有数据';
            }
            while (count($res) > 0) {
                $this->notifyUpdate($res);
                $res = $this->getOrder(100);
                echo '通知成功';
            }

        } catch (\Exception $exception) {
            echo  '异常通知';
        }

    }
    //域名到期提醒
    public function domain(){
        //找出今天没有通知的
        $res=$this->getDomain(100);

        try {
            if(empty($res))
            {
                echo '没有数据';
            }
            while (count($res) > 0) {
                $this->notifyUpdateDomain($res);
                $res = $this->getOrder(100);
                echo '通知成功';
            }

        } catch (\Exception $exception) {
            echo  '异常通知';
        }
    }
    public function notifyUpdate($items){
        $to_day=date('Y-m-d');
        $notify_type=config_cache_default('host_server.email_end_day_numbers',1);

        if(empty($items))
        {
            return false;
        }
        foreach ($items as $k=>$v)
        {
            if($notify_type==2)
            {
                if($v['notify_day'])
                {
                    //更新
                    PnHostSvOrder::where('id',$v['id'])->update(['notify_day'=>$to_day]);
                    continue;
                }
            }
            $email=$v['user']['email'];
            $data=$v;
            $data['type']='user_notify';
            NotifyService::orderNotifyUser($email,'服务器到期提醒',$data);
            //站长通知
            $data['type']='user_notify_site';
            NotifyService::orderNotifySiteUser('会员服务器到期提醒',$data);
            //更新
            PnHostSvOrder::where('id',$v['id'])->update(['notify_day'=>$to_day]);
        }
    }
    //取得到期7天域名
    public function getDomain($numbers){
        $to_day=date('Y-m-d');
        $number=config_cache_default('host_server.email_end_day',7);
        $limit_day=DateServices::incrDay($number);
        $orders=PnHostSvDomainOrder::with('user')->whereRaw('(notify_day is null or notify_day < ?)',[$to_day])
            ->whereIn('status',[1,3,4])->where('end_at','<=',$limit_day)->limit($numbers)->get()->toArray();
        return $orders;
    }
    //域名更新
    public function notifyUpdateDomain($items){
        $to_day=date('Y-m-d');
        $notify_type=config_cache_default('host_server.email_end_day_numbers',1);

        if(empty($items))
        {
            return false;
        }
        foreach ($items as $k=>$v)
        {
            if($notify_type==2)
            {
                if($v['notify_day'])
                {
                    //更新
                    PnHostSvDomainOrder::where('id',$v['id'])->update(['notify_day'=>$to_day]);
                    continue;
                }
            }
            $email=$v['user']['email'];
            $data=$v;
            $data['type']='user_domain_notify';
            NotifyService::orderNotifyUser($email,$v['name'].'域名到期提醒',$data);
            //站长通知
            $data['type']='user_domain_notify_site';
            NotifyService::orderNotifySiteUser('会员域名'.$v['name'].'到期提醒',$data);
            //更新
            PnHostSvDomainOrder::where('id',$v['id'])->update(['notify_day'=>$to_day]);
        }
    }
    public function getOrder($numbers){
        $to_day=date('Y-m-d');
        $number=config_cache_default('host_server.email_end_day',7);
        $limit_day=DateServices::incrDay($number);
        $orders=PnHostSvOrder::with('user')->whereRaw('(notify_day is null or notify_day < ?)',[$to_day])
            ->where('status',1)->where('end_at','<=',$limit_day)->limit($numbers)->get()->toArray();
        return $orders;
    }

}